using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowProcesses = Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowRelevantData = Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;
using WorkflowParticipantsData = Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData;
using WorkflowDataTypes = Scenario.WorkflowEngine.DataModel.WorkflowDataTypes;

using WAPI = Scenario.WAPI;

namespace Scenario.Modeler.Forms
{
	/// <summary>
	/// Descrizione di riepilogo per ProcessProperties.
	/// </summary>
	public class ProcessInstanceManager : System.Windows.Forms.Form
	{
		public WorkflowProcesses.XMLWorkflow xmlWorkflow = null;

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.TextBox txtProcessId;
		private System.Windows.Forms.Label Id;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox cmbActivities;
		private System.Windows.Forms.ComboBox cmbValues;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ComboBox cmbUsers;
		private System.Windows.Forms.ListView lstActivities;
		private System.Windows.Forms.Button btnLoad;
		private System.Windows.Forms.Button btnLoadInCanvas;
		private System.Windows.Forms.ListView lstAttributes;
		private System.Windows.Forms.ListView lstActivityAttribute;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private ArrayList vAttributesActivityModified = new ArrayList();

		public ProcessInstanceManager(string sName, ref WorkflowProcesses.XMLWorkflows workflows)
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//
			if ( workflows != null )
			{
				for ( int i=0; i<workflows.Count;i++)
				{
					if ( ((WorkflowProcesses.XMLWorkflow)workflows[i]).Name == sName )
					{
						xmlWorkflow = (WorkflowProcesses.XMLWorkflow)workflows[i];

						this.txtName.Text = xmlWorkflow.Name;

						try
						{
							WorkflowRelevantData.XMLAttribute attr = xmlWorkflow.XMLAttributes.Get("WorkflowProcess.Id");
							this.txtProcessId.Text = attr.Value;

							this.MakeGrids(xmlWorkflow);
						}
						catch 
						{
						}
					}
				}
			}

            WorkflowParticipantsData.XMLUsers xmlUsers = WorkflowEngine.Engine.UserHelper.GetUsers();

			foreach ( WorkflowParticipantsData.XMLUser user in xmlUsers)
			{
				this.cmbUsers.Items.Add( user.Key ) ;
			}
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtName = new System.Windows.Forms.TextBox();
			this.panel2 = new System.Windows.Forms.Panel();
			this.btnLoadInCanvas = new System.Windows.Forms.Button();
			this.btnLoad = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.txtProcessId = new System.Windows.Forms.TextBox();
			this.Id = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.cmbActivities = new System.Windows.Forms.ComboBox();
			this.label3 = new System.Windows.Forms.Label();
			this.cmbValues = new System.Windows.Forms.ComboBox();
			this.label4 = new System.Windows.Forms.Label();
			this.cmbUsers = new System.Windows.Forms.ComboBox();
			this.lstActivities = new System.Windows.Forms.ListView();
			this.lstAttributes = new System.Windows.Forms.ListView();
			this.lstActivityAttribute = new System.Windows.Forms.ListView();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(592, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Name";
			// 
			// txtName
			// 
			this.txtName.Enabled = false;
			this.txtName.Location = new System.Drawing.Point(16, 32);
			this.txtName.Name = "txtName";
			this.txtName.Size = new System.Drawing.Size(592, 22);
			this.txtName.TabIndex = 1;
			this.txtName.Text = "";
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.btnCancel);
			this.panel2.Controls.Add(this.btnSave);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel2.Location = new System.Drawing.Point(0, 680);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(1208, 56);
			this.panel2.TabIndex = 15;
			// 
			// btnLoadInCanvas
			// 
			this.btnLoadInCanvas.Font = new System.Drawing.Font("Garamond", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnLoadInCanvas.Location = new System.Drawing.Point(352, 80);
			this.btnLoadInCanvas.Name = "btnLoadInCanvas";
			this.btnLoadInCanvas.Size = new System.Drawing.Size(256, 40);
			this.btnLoadInCanvas.TabIndex = 12;
			this.btnLoadInCanvas.Text = "LOAD Process In Canvas";
			this.btnLoadInCanvas.Click += new System.EventHandler(this.btnLoadInCanvas_Click);
			// 
			// btnLoad
			// 
			this.btnLoad.Font = new System.Drawing.Font("Garamond", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnLoad.Location = new System.Drawing.Point(120, 80);
			this.btnLoad.Name = "btnLoad";
			this.btnLoad.Size = new System.Drawing.Size(192, 40);
			this.btnLoad.TabIndex = 11;
			this.btnLoad.Text = "LOAD Process";
			this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Font = new System.Drawing.Font("Garamond", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnCancel.Location = new System.Drawing.Point(624, 8);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(104, 40);
			this.btnCancel.TabIndex = 10;
			this.btnCancel.Text = "CLOSE";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnSave
			// 
			this.btnSave.Font = new System.Drawing.Font("Garamond", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnSave.Location = new System.Drawing.Point(304, 8);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(184, 40);
			this.btnSave.TabIndex = 9;
			this.btnSave.Text = "RESTART Process";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// txtProcessId
			// 
			this.txtProcessId.Location = new System.Drawing.Point(16, 88);
			this.txtProcessId.Name = "txtProcessId";
			this.txtProcessId.Size = new System.Drawing.Size(88, 22);
			this.txtProcessId.TabIndex = 17;
			this.txtProcessId.Text = "";
			// 
			// Id
			// 
			this.Id.Location = new System.Drawing.Point(16, 72);
			this.Id.Name = "Id";
			this.Id.Size = new System.Drawing.Size(96, 24);
			this.Id.TabIndex = 16;
			this.Id.Text = "Id";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 136);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(136, 16);
			this.label2.TabIndex = 18;
			this.label2.Text = "Restart From";
			// 
			// cmbActivities
			// 
			this.cmbActivities.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbActivities.Location = new System.Drawing.Point(16, 152);
			this.cmbActivities.Name = "cmbActivities";
			this.cmbActivities.Size = new System.Drawing.Size(584, 24);
			this.cmbActivities.Sorted = true;
			this.cmbActivities.TabIndex = 19;
			this.cmbActivities.SelectedIndexChanged += new System.EventHandler(this.cmbActivities_SelectedIndexChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 192);
			this.label3.Name = "label3";
			this.label3.TabIndex = 20;
			this.label3.Text = "Result";
			// 
			// cmbValues
			// 
			this.cmbValues.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbValues.Location = new System.Drawing.Point(16, 208);
			this.cmbValues.Name = "cmbValues";
			this.cmbValues.Size = new System.Drawing.Size(584, 24);
			this.cmbValues.Sorted = true;
			this.cmbValues.TabIndex = 21;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 248);
			this.label4.Name = "label4";
			this.label4.TabIndex = 22;
			this.label4.Text = "Users";
			// 
			// cmbUsers
			// 
			this.cmbUsers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbUsers.Location = new System.Drawing.Point(16, 264);
			this.cmbUsers.Name = "cmbUsers";
			this.cmbUsers.Size = new System.Drawing.Size(584, 24);
			this.cmbUsers.Sorted = true;
			this.cmbUsers.TabIndex = 23;
			// 
			// lstActivities
			// 
			this.lstActivities.Location = new System.Drawing.Point(16, 296);
			this.lstActivities.Name = "lstActivities";
			this.lstActivities.Size = new System.Drawing.Size(584, 368);
			this.lstActivities.TabIndex = 24;
			this.lstActivities.View = System.Windows.Forms.View.Details;
			this.lstActivities.DoubleClick += new System.EventHandler(this.lstActivities_DoubleClick);
			// 
			// lstAttributes
			// 
			this.lstAttributes.FullRowSelect = true;
			this.lstAttributes.Location = new System.Drawing.Point(616, 32);
			this.lstAttributes.Name = "lstAttributes";
			this.lstAttributes.Size = new System.Drawing.Size(584, 376);
			this.lstAttributes.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.lstAttributes.TabIndex = 25;
			this.lstAttributes.View = System.Windows.Forms.View.Details;
			this.lstAttributes.DoubleClick += new System.EventHandler(this.lstAttributes_DoubleClick);
			// 
			// lstActivityAttribute
			// 
			this.lstActivityAttribute.FullRowSelect = true;
			this.lstActivityAttribute.Location = new System.Drawing.Point(616, 416);
			this.lstActivityAttribute.Name = "lstActivityAttribute";
			this.lstActivityAttribute.Size = new System.Drawing.Size(584, 256);
			this.lstActivityAttribute.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.lstActivityAttribute.TabIndex = 26;
			this.lstActivityAttribute.View = System.Windows.Forms.View.Details;
			this.lstActivityAttribute.DoubleClick += new System.EventHandler(this.lstActivityAttribute_DoubleClick);
			// 
			// ProcessInstanceManager
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(1208, 736);
			this.Controls.Add(this.lstActivityAttribute);
			this.Controls.Add(this.lstAttributes);
			this.Controls.Add(this.lstActivities);
			this.Controls.Add(this.cmbUsers);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.cmbValues);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.cmbActivities);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtProcessId);
			this.Controls.Add(this.txtName);
			this.Controls.Add(this.Id);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.btnLoad);
			this.Controls.Add(this.btnLoadInCanvas);
			this.Name = "ProcessInstanceManager";
			this.Text = "ProcessInstance";
			this.Load += new System.EventHandler(this.ProcessInstanceManager_Load);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.xmlWorkflow = null;
			this.Close();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
            WorkflowEngine.Engine.ProcessHelper.SetAttributes(this.txtProcessId.Text, this.xmlWorkflow.XMLAttributes);
            
			for ( int i=0; i<this.vAttributesActivityModified.Count;i++)
			{
				string sActivityName = (string)this.vAttributesActivityModified[i];
				WorkflowProcesses.XMLActivity xmlActivity = xmlWorkflow.XMLActivities.Get (sActivityName);

                WorkflowEngine.Engine.ActivityHelper.SetAttributes(this.txtProcessId.Text,
                                                                    xmlActivity.Name,
                                                                    xmlActivity.XMLAttributes,
                                                                    this.cmbUsers.SelectedItem.ToString());
			}

            WorkflowEngine.Engine.ActivityHelper.PlayActivity(this.txtProcessId.Text,
                                                              this.cmbActivities.SelectedItem.ToString(),
                                                              this.cmbUsers.SelectedItem.ToString(),
                                                              WAPI.ActivityHelper.ActivityOperation.Complete,
                                                              this.cmbValues.SelectedItem.ToString());

		}

		private void cmbActivities_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			WorkflowProcesses.XMLActivity activity = xmlWorkflow.XMLActivities.Get (this.cmbActivities.SelectedItem.ToString());
			
            Scenario.WorkflowEngine.DataModel.WorkflowDataTypes.XMLEnumerationValues vValues= WorkflowEngine.Engine.DataTypesHelper.GetValues(activity.XMLBehaviour.XMLBehaviourExecution.XMLScript.DataType);
            for (int i = 0; i < vValues.Count; i++)
			{
                WorkflowDataTypes.XMLDataTypeValue dtValue = (WorkflowDataTypes.XMLDataTypeValue)vValues[i];

				this.cmbValues.Items.Add (dtValue.Key);
			}

			if ( this.lstActivityAttribute.Columns.Count == 0 )
			{
				lstActivityAttribute.Columns.Add("Name",100, HorizontalAlignment.Left);
				lstActivityAttribute.Columns.Add ("Value", lstActivityAttribute.Width - 100, HorizontalAlignment.Left);
			}
			else
			{
				this.lstActivityAttribute.Items.Clear();
			}

			foreach ( WorkflowRelevantData.XMLAttribute xmlAttribute in activity.XMLAttributes)
			{
				ListViewItem item = new ListViewItem();
				item.Text = xmlAttribute.Name;
				item.SubItems.Add(xmlAttribute.Value);
				this.lstActivityAttribute.Items.Add(item);
			}
		}

		private void MakeGrids(WorkflowProcesses.XMLWorkflow xmlWorkflow)
		{
			if ( this.lstActivities.Columns.Count == 0 )
			{
				ColumnHeader ch = new ColumnHeader();
				ch.Text = "Name";
				ch.TextAlign = HorizontalAlignment.Right;
				ch.Width = 100;
				this.lstActivities.Columns.Add(ch);

				ch = new ColumnHeader();
				ch.Text = "Status";
				ch.TextAlign = HorizontalAlignment.Left;
				ch.Width = lstActivities.Width - 100;
				this.lstActivities.Columns.Add(ch);
			}
			else
			{
				this.lstActivities.Items.Clear();
			}

			this.cmbActivities.Items.Clear();

			if ( this.lstAttributes.Columns.Count == 0 )
			{
				lstAttributes.Columns.Add("Name",100, HorizontalAlignment.Left);
				lstAttributes.Columns.Add ("Value", lstAttributes.Width - 100, HorizontalAlignment.Left);
			}
			else
			{
				this.lstAttributes.Items.Clear();
			}


			foreach ( WorkflowProcesses.XMLActivity activity in xmlWorkflow.XMLActivities)
			{
				this.cmbActivities.Items.Add ( activity.Name);

				ListViewItem item = new ListViewItem();
				item.Text = activity.Name;
				item.SubItems.Add(activity.XMLLog.Status);
				this.lstActivities.Items.Add(item);
			}

			foreach ( WorkflowRelevantData.XMLAttribute xmlAttribute in xmlWorkflow.XMLAttributes)
			{
				ListViewItem item = new ListViewItem();
				item.Text = xmlAttribute.Name;
				item.SubItems.Add(xmlAttribute.Value);
				this.lstAttributes.Items.Add(item);
			}
		}

		private WorkflowProcesses.XMLWorkflow LoadProcess(long lProcessId)
		{
            WorkflowProcesses.XMLWorkflow xmlWorkflow = WorkflowEngine.Engine.ProcessHelper.GetWorkflowInstance(lProcessId.ToString());

			return xmlWorkflow;
		}

		private void btnLoad_Click(object sender, System.EventArgs e)
		{
			if ( this.txtProcessId.Text.CompareTo (string.Empty ) == 0)
			{
				MessageBox.Show ("It's Mandatory to tell WorkflowProcess Id");
			}
			else
			{
				this.xmlWorkflow  = this.LoadProcess(Int32.Parse ( this.txtProcessId.Text));

				this.MakeGrids ( this.xmlWorkflow);
			}
		}

		private void btnLoadInCanvas_Click(object sender, System.EventArgs e)
		{
			if ( this.txtProcessId.Text.CompareTo (string.Empty ) == 0)
			{
				MessageBox.Show ("It's Mandatory to tell WorkflowProcess Id");
			}
			else
			{
				this.xmlWorkflow  = this.LoadProcess(Int32.Parse ( this.txtProcessId.Text));
				this.Close();
			}
		}

		private void ProcessInstanceManager_Load(object sender, System.EventArgs e)
		{
		
		}

		private void lstAttributes_DoubleClick(object sender, System.EventArgs e)
		{
			if ( lstAttributes.SelectedItems.Count == 0 )
			{
				return;
			}

			WorkflowRelevantData.XMLAttribute xmlAttribute = this.xmlWorkflow.XMLAttributes.Get(this.lstAttributes.SelectedItems[0].SubItems[0].Text);
			FormAttribute form = new FormAttribute(xmlAttribute);
			form.ShowDialog (this);

			if ( form.xmlAttribute != null )
			{
				lstAttributes.SelectedItems[0].SubItems[0].Text = form.xmlAttribute.Name;
				lstAttributes.SelectedItems[0].SubItems[1].Text = form.xmlAttribute.Value;

				xmlAttribute.Name = form.xmlAttribute.Name;
				xmlAttribute.Value = form.xmlAttribute.Value;
			}
		}

		private void lstActivityAttribute_DoubleClick(object sender, System.EventArgs e)
		{
			WorkflowProcesses.XMLActivity xmlActivity = xmlWorkflow.XMLActivities.Get (this.cmbActivities.SelectedItem.ToString());

			if ( lstAttributes.SelectedItems.Count == 0 )
			{
				return;
			}

			

			WorkflowRelevantData.XMLAttribute xmlAttribute = xmlActivity.XMLAttributes.Get(this.lstActivityAttribute.SelectedItems[0].SubItems[0].Text);
			FormAttribute form = new FormAttribute(xmlAttribute);
			form.ShowDialog (this);

			if ( form.xmlAttribute != null )
			{
				lstActivityAttribute.SelectedItems[0].SubItems[0].Text = form.xmlAttribute.Name;
				lstActivityAttribute.SelectedItems[0].SubItems[1].Text = form.xmlAttribute.Value;

				xmlAttribute.Name = form.xmlAttribute.Name;
				xmlAttribute.Value = form.xmlAttribute.Value;
			}

			vAttributesActivityModified.Add (xmlActivity.Name);
		}

		private void lstActivities_DoubleClick(object sender, System.EventArgs e)
		{
			WorkflowProcesses.XMLActivity activity = xmlWorkflow.XMLActivities.Get (this.lstActivities.SelectedItems[0].SubItems[0].Text);
			if ( this.lstActivityAttribute.Columns.Count == 0 )
			{
				lstActivityAttribute.Columns.Add("Name",100, HorizontalAlignment.Left);
				lstActivityAttribute.Columns.Add ("Value", lstActivityAttribute.Width - 100, HorizontalAlignment.Left);
			}
			else
			{
				this.lstActivityAttribute.Items.Clear();
			}

			foreach ( WorkflowRelevantData.XMLAttribute xmlAttribute in activity.XMLAttributes)
			{
				ListViewItem item = new ListViewItem();
				item.Text = xmlAttribute.Name;
				item.SubItems.Add(xmlAttribute.Value);
				this.lstActivityAttribute.Items.Add(item);
			}
		}
	}
}
